package websocket;


import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;



import org.java_websocket.WebSocket;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft;
import org.java_websocket.drafts.Draft_17;
import org.java_websocket.handshake.ServerHandshake;

/**
 * websocket测试用客户端
 * @author huanghaiyun
 * @createTime 2016-1-15上午10:55:49
 */
public class WebsocketClientTest extends WebSocketClient {

	
	
	public WebsocketClientTest( Draft d , URI uri ) {
		super(uri,d);
	}
	/**
	 * @param args
	 */
	

	@Override
	public void onMessage( String message ) {
//		if(message.startsWith("telnet")){
			System.out.println(this.toString()+"\t"+message);
//		}if(message.startsWith("Dog")){
//			System.out.println(this.toString()+"\t加密狗消息"+"\t"+message);
//		}
	}
	@Override
	public void onError( Exception ex ) {
		System.out.println( "Error: " );
		ex.printStackTrace();
	}

	@Override
	public void onOpen( ServerHandshake handshake ) {
		send("hello world");
		System.out.println("连接已打开！");
	}

	@Override
	public void onClose( int code, String reason, boolean remote ) {
		System.out.println( "Closed: " + code + " " + reason );
	}
	public static void main( String[] args ) {

		try {
			WebsocketServerTest ws=new WebsocketServerTest(new InetSocketAddress(9001),new Draft_17());
			ws.start();
			
			Draft d = new Draft_17();
			String uri="ws://localhost:9001";
			WebsocketClientTest wct1 = null;
			WebsocketClientTest wct2 = null;
			try {
				wct1 = new WebsocketClientTest(d, new URI(uri));
				wct2 = new WebsocketClientTest(d, new URI(uri));
			} catch (URISyntaxException e) {
				e.printStackTrace();
			}
			wct1.connect();
			wct2.connect();
			MsgContainer mp=MsgContainer.getInstance();
			NotifyMsg notifyMsg=new NotifyMsg("Dog", "String","你好");
			mp.send(notifyMsg);
			//			js.send("通告：hello");
//			WSTelnetClient telnet=new WSTelnetClient("10.60.0.200",23,"wifiadmin","WF-ieee802.11","VT220","#");
//			List< WebSocket> wl=new ArrayList<WebSocket>();
//			wl.add(ws.getList().get(1));
//			MsgProducer js1=new MsgProducer<WebSocket>(telnet,wl);
//			js1.send(telnet.login());
			
		} catch (Exception e) {
			
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 

	}

}
